package com.download.web.controller;

import com.blade.exception.ValidatorException;
import com.blade.mvc.ui.RestResponse;
import com.download.util.UploadUtil;
import com.download.web.entity.BusOrder;
import com.download.web.entity.model.Result;
import com.download.web.pay.MakeMoneyService;
import com.download.web.pay.entity.PayOrderResponse;
import com.download.web.pay.enums.PlatFormEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: cmy
 * @create: 2019-03-09 09:39
 **/
@Slf4j
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private MakeMoneyService makeMoneyService;
    @Value("${upload.image.src}")
    private String src;

    /**
     * 创建支付订单
     *
     * @param order
     * @return
     */
    @RequestMapping("/youzan")
    public Result<PayOrderResponse> createOrder(BusOrder order) {
        PayOrderResponse payOrderResponse=null;
        try {
//            Validator.orderParam(order);
           /* if (null == request.header("") || null == request.userAgent()) {
                return RestResponse.fail("非法请求");
            }*/
            payOrderResponse=makeMoneyService.createOrder(order);
        } catch (Exception e) {
            if (!(e instanceof ValidatorException)) {
                log.error("支付出现异常", e);
            }
            return Result.error(e.getMessage());
        }
        return Result.ok(payOrderResponse);
    }

    @RequestMapping("/youzan/payCallback")
    public void payCallback(String body) {
        try {
//            Validator.orderParam(order);
           /* if (null == request.header("") || null == request.userAgent()) {
                return RestResponse.fail("非法请求");
            }*/
            makeMoneyService.execCallback(PlatFormEnum.YOUZAN, body);
        } catch (Exception e) {
            if (!(e instanceof ValidatorException)) {
                log.error("支付出现异常", e);
            }
//            return RestResponse.fail(e.getMessage());
        }
    }


    @RequestMapping("/uploadImage")
    public Result uploadImage(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Result result = new Result();

        String path=UploadUtil.upload(src,file);

        result.setCode(0);
        result.setMsg("图片上传成功");
        Map<String, Object> map = new HashMap<>();
        map.put("src",path);
        result.setData(map);


        return result;
    }
}
